package bootTest.redis;

import bootTest.util.SerializeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.nio.charset.Charset;

@Component
public class JedisUtils {

    private static final Charset UTF8 = Charset.forName("UTF-8");
    @Autowired
    private JedisPool jedisPool;


    public final void set(String key, Object obj) throws Exception{
        Jedis resource = jedisPool.getResource();
        try (resource) {
            resource.set(key.getBytes(UTF8), SerializeUtil.serialize(obj));
        }
    }

    public final Object get(String key) throws Exception {
        Jedis resource = jedisPool.getResource();
        try (resource) {
            byte[] bytes = resource.get(key.getBytes(UTF8));
            return SerializeUtil.unserialize(bytes);
        }
    }
}
